package com.jinxinol.basic.system.web;

import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import com.jinxinol.basic.system.domain.SysRight;
import com.jinxinol.basic.system.domain.SysRole;
import com.jinxinol.basic.system.domain.SysSubsystem;
import com.jinxinol.basic.system.domain.SysUser;
import com.jinxinol.basic.system.service.ISysRightService;
import com.jinxinol.basic.system.service.ISysSubsystemService;
import com.jinxinol.basic.system.service.ISysUserService;
import com.jinxinol.core.common.BaseAction;
import com.jinxinol.core.common.ContextHelper;
import com.jinxinol.core.common.PageModel;
/**
 * 
 * @company Jinxin Computer Corp.
 * @author Conner
 * @email 764828388@qq.com
 * @description 权限资源Action
 * @date 2012-9-5
 */
public class RightAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static final Log logger = ContextHelper.getLogger(RightAction.class);
	
	private SysRight right = new SysRight();
	private SysSubsystem subSystem = new SysSubsystem();
	private PageModel<SysRight> pm = new PageModel<SysRight>();
	private List<SysRight> list;
	private List<SysSubsystem> listSubSystem;
	private Integer id;  //权限ID
	private String type;
	private String idStr;
	private String subSysName;
	private int i = 0;
	private int j = 0;
	
	@Resource 
	ISysRightService rightService;
	
	@Resource
	ISysSubsystemService sysSubsystemService;
	
	public RightAction(){}
	
	/**
	 * 添加权限
	 * */
	public String add(){
		try {
			rightService.insert(right);
			return "addSuccess";
		} catch (Exception e) {
			logger.error("新增权限"+right.getName() +"失败", e);
			return ERROR;
		}
	}
	
	/**
	 * 查询所有子系统信息
	 */
	public String findAllRightSubSys(){
		try {
			listSubSystem = sysSubsystemService.find(subSystem);  //查询所有子系统信息
			return "findAllRightSubSysSuccess";
		} catch (Exception e) {
			logger.error("查询所有子系统信息失败", e);
			return ERROR;
		}
	}
	
	/**
	 * 根据子系统id，查询该子系统的所有权限资源
	 */
	public void findAllRightBySubId(){
		try {
			list = rightService.find(right);
			if(list != null && list.size() > 0){
				for(i=0;i<list.size();i++){
					list.get(i).setpId(list.get(i).getParent_id());
				}
			}
			jsonObject.put(RESULT_CODE, SUCCESS);
			jsonObject.put("list", list);
		} catch (Exception e) {
			logger.error("加载权限资源信息失败！", e);
			jsonObject.put(RESULT_CODE, ERROR);
		}
		
		this.pushJsonResult();
	}
	
	/**
	 * 删除权限
	 * */
	public String delete(){
		String[] arr = null;
		try {
			if(!idStr.equals("")){
				arr = idStr.split(",");
				for(i = 0; i < arr.length ; i++){
					right = new SysRight();
					right.setId(Integer.parseInt(arr[i]));
					rightService.delete(right);
				}
			}
			logger.info("删除权限成功！");
			return "delSuccess";
		} catch (Exception e) {
			logger.error("删除权限出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 修改权限信息
	 * */
	public String update(){
		try {
			rightService.update(right);
			return "updSuccess";
		} catch (Exception e) {
			logger.error("修改权限"+right.getName()+"出现异常！", e);
			return ERROR;
		}
	}
	
	/**
	 * 修改权限状态
	 * */
	public void updateRightStatus(){
		try {
			rightService.update(right);
			jsonObject.put(RESULT_CODE, SUCCESS);
		} catch (Exception e) {
			logger.error("修改权限"+right.getName()+"状态出现异常！", e);
			jsonObject.put(RESULT_CODE, ERROR);
			jsonObject.put(RESULT_MSG, "改变权限状态失败，请联系管理员！");
		}
		this.pushJsonResult();
	}
	
	/**
	 * 按ID查询权限
	 * */
	public String queryByRid(){
		String sub_name = null;  //子系统名称
		String parent_name = null;  //父节点名称
		Integer sub_id = null;
		Integer parent_id = null;
		SysRight right2 = null;
		try {
			right = rightService.findById(right.getId());
			right2 = new SysRight();
			right2.setSub_id(right.getSub_id());
			list = rightService.find(right2);  //查询所有子系统权限
			sub_id = right.getSub_id();
			parent_id = right.getParent_id();
			listSubSystem = sysSubsystemService.find(subSystem);
			//将权限所属子系统名称,置入实体
			if(list != null && list.size() > 0 && right != null){
				for(i = 0 ; i < list.size() ; i++){
					if(parent_id == list.get(i).getId()){
						parent_name = list.get(i).getName();
						right.setParent_name(parent_name);
						break;
					}
				}
			}
			//将权限所属子系统名称,置入实体
			if(listSubSystem != null && listSubSystem.size() > 0 && right != null){
				for(i = 0 ; i < listSubSystem.size() ; i++){
					if(sub_id == listSubSystem.get(i).getId()){
						sub_name = listSubSystem.get(i).getName();
						right.setSub_name(sub_name);
						break;
					}
				}
			}
			if(type.equals("detail")){
				return "qurSuccess";
			}else if(type.equals("edit")){
				return "qurSuccess2";
			}else{
				return "qurSuccess";
			}
			
		} catch (Exception e) {
			logger.error("按ID查询权限出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 按条件查询权限
	 * */
	public String selectRight(){
		try {
			pm = rightService.findOnPage(right, pm);
			listSubSystem = sysSubsystemService.find(subSystem);  //查询所有子系统信息
			//设置子系统名称
			if(pm.getList() != null && pm.getList().size() > 0 &&
					listSubSystem != null && listSubSystem.size() > 0){
				for(i = 0 ; i < pm.getList().size() ; i++){
					for(j = 0 ; j < listSubSystem.size() ; j++){
						if(pm.getList().get(i).getSub_id() == listSubSystem.get(j).getId()){
							subSysName = listSubSystem.get(j).getName();
							pm.getList().get(i).setSub_name(subSysName);
						}
					}
				}
			}
			return "list";
		} catch (Exception e) {
			logger.error("按条件查询权限出现异常", e);
			return ERROR;
		}
	}
	
	//getter & setter
	public SysRight getRight() {
		return right;
	}
	
	public void setRight(SysRight right) {
		this.right = right;
	}
	
	public PageModel<SysRight> getPm() {
		return pm;
	}
	
	public void setPm(PageModel<SysRight> pm) {
		this.pm = pm;
	}
	
	public List<SysRight> getList() {
		return list;
	}
	
	public void setList(List<SysRight> list) {
		this.list = list;
	}

	public List<SysSubsystem> getListSubSystem() {
		return listSubSystem;
	}

	public void setListSubSystem(List<SysSubsystem> listSubSystem) {
		this.listSubSystem = listSubSystem;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getIdStr() {
		return idStr;
	}

	public void setIdStr(String idStr) {
		this.idStr = idStr;
	}
}
